package com.bmob.lover.app.util

import com.effective.android.anchors.task.TaskCreator
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.effective.android.anchors.task.Task
import com.effective.android.anchors.task.project.Project
import com.hjq.toast.ToastUtils
import com.qmuiteam.qmui.arch.QMUISwipeBackActivityManager
import com.shuyu.gsyvideoplayer.cache.CacheFactory
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.utils.Debuger
import com.tencent.mmkv.MMKV
import com.theone.common.ext.LogInit
import com.bmob.lover.data.constant.API_KEY
import com.bmob.lover.data.constant.APP_ID
import com.bmob.lover.ui.activity.ErrorActivity
import com.bmob.lover.ui.activity.LauncherActivity
import com.theone.mvvm.base.BaseApplication
import com.theone.mvvm.base.appContext
import com.theone.mvvm.core.app.ext.initLoaderDefault
import com.theone.mvvm.core.app.util.MMKVUtil
import com.theone.mvvm.core.app.util.NotificationManager
import com.theone.mvvm.core.app.util.RxHttpManager
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager
import tv.danmaku.ijk.media.exo2.ExoPlayerCacheManager

/**
 * 作者　: hegaojian
 * 时间　: 2020/11/17
 * 描述　:
 */
object TaskCreator : TaskCreator {
    override fun createTask(taskName: String): Task {
        return when (taskName) {
            InitNetWork.TASK_ID -> InitNetWork()
            InitComm.TASK_ID -> InitComm()
            InitUtils.TASK_ID -> InitUtils()
            InitToast.TASK_ID -> InitToast()
            else -> InitDefault()
        }
    }
}

class InitDefault : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "0"
    }
    override fun run(name: String) {

    }
}


/**
 * 初始化网络
 */
class InitNetWork : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "1"
    }
    override fun run(name: String) {
        RxHttpManager.init().setDebug(BaseApplication.DEBUG).setOnParamAssembly {
            //添加公共请求头
            it.addHeader("X-Bmob-Application-Id", APP_ID)
                .addHeader("X-Bmob-REST-API-Key", API_KEY)
                .addHeader("Content-Type", "application/json")
        }
    }
}

//初始化常用控件类
class InitComm : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "2"
    }

    override fun run(name: String) {
        QMUISwipeBackActivityManager.init(appContext)
        initLoaderDefault()
        NotificationManager.getInstance().register()
        initGSYVideoPlayer()
    }
}

//初始化Utils
class InitUtils : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "3"
    }

    override fun run(name: String) {
        LogInit(BaseApplication.DEBUG)
        NotificationManager.getInstance().register()

    }
}

//初始化Utils
class InitToast : Task(TASK_ID, false) {
    companion object {
        const val TASK_ID = "4"
    }

    override fun run(name: String) {
        initCrashConfig()
        //初始化吐司 这个吐司必须要主线程中初始化
        ToastUtils.init(appContext)
        MMKVUtil.init(appContext,"bmob_mmkv")
        // 再次安装后请求时要清除，不然会读取到
        if (CacheUtil.isFirst()) {
            RxHttpManager.clearCache()
            CacheUtil.isEnterApp()
        }
    }
}

class AppTaskFactory : Project.TaskFactory(TaskCreator)


private fun initCrashConfig() {
    //防止项目崩溃，崩溃后打开错误界面
    CaocConfig.Builder.create()
        .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
        .enabled(true)//是否启用CustomActivityOnCrash崩溃拦截机制 必须启用！不然集成这个库干啥？？？
        .trackActivities(true) //是否必须跟踪用户访问的活动及其生命周期调用 default: false
        .minTimeBetweenCrashesMs(2000) //应用程序崩溃之间必须经过的时间 default: 3000
        .restartActivity(LauncherActivity::class.java) // 重启的activity
        .errorActivity(ErrorActivity::class.java)
        .apply()
}

private fun initGSYVideoPlayer() {
    if (BaseApplication.DEBUG) {
        Debuger.enable()
    }
    PlayerFactory.setPlayManager(Exo2PlayerManager::class.java) //EXO模式
    CacheFactory.setCacheManager(ExoPlayerCacheManager::class.java) //exo缓存模式，支持m3u8，只支持exo
}